﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace CodeCollection
{
    class PublicFunction
    {

        #region aa

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string ReadCodeFromFile(string filePath)
        {
            string strRet = "";
            if (File.Exists(filePath))
            {
                using (FileStream FS = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read))
                {
                    using (BinaryReader br = new BinaryReader(FS))
                    {
                        br.BaseStream.Seek(0, SeekOrigin.Begin);
                        byte[] buffer = br.ReadBytes((int)br.BaseStream.Length);
                        strRet = Encoding.Unicode.GetString(buffer);
                        br.Close();
                        FS.Close();
                    }
                }
            }
            return DecryptDES(strRet);
        }
        /// <summary>
        /// 保存注册码
        /// </summary>
        /// <param name="strCode"></param>
        public static void WriteCodeToFile(string filePath, string strCode)
        {
            using (FileStream FS = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                using (BinaryWriter bw = new BinaryWriter(FS))
                {
                    byte[] buffer = Encoding.Unicode.GetBytes(EncryptDES(strCode));
                    bw.BaseStream.Seek(0, SeekOrigin.Begin);
                    bw.Write(buffer);
                    bw.Close();
                    FS.Close();
                }
            }
        }
        #endregion

        #region 加密解密
        /// <summary>
        /// 加密密钥
        /// </summary>
        private const string DES_KEYS = "loveyanp";
        /// <summary>
        /// 加密密钥
        /// </summary>
        private static byte[] DESKeys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="encryptString">需要加密的字符串</param>
        /// <returns></returns>
        public static string EncryptDES(string encryptString)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(DES_KEYS.Substring(0, 8));
                byte[] rgbIV = DESKeys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                dCSP.Mode = CipherMode.ECB;
                dCSP.Padding = PaddingMode.None;
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="decryptString"></param>
        /// <returns></returns>
        public static string DecryptDES(string decryptString)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(DES_KEYS.Substring(0, 8));
                byte[] rgbIV = DESKeys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                DCSP.Mode = CipherMode.ECB;
                DCSP.Padding = PaddingMode.None;
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }

        #endregion
        /// <summary>
        /// 新建文件夹
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        /// <returns>true：新建成功或者是文件夹存在</returns>
        public static bool CreateFolder(string folderPath)
        {
            //如果文件夹不存在，则新建
            if (!Directory.Exists(folderPath))
            {
                try
                {
                    Directory.CreateDirectory(folderPath);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
        public static bool DeleteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                try
                {
                    File.Delete(filePath);
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
        public static bool DeleteDirectory(string dirPath)
        {
            if (Directory.Exists(dirPath))
            {
                try
                {
                    Directory.Delete(dirPath,true);
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
        public static bool DeletePath(string strPath)
        {
            bool bRet = DeleteFile(strPath);
            if (!bRet)
            {
                return false;
            }
            return DeleteDirectory(strPath);
        }
    }
}
